/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perutaxi.datos;

import com.perutaxi.entidades.eRol;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author PeruTaxi
 */
public class dRol implements Serializable {

    public static eRol inicializar() {
        eRol rol = new eRol();
        rol.setNombre("");
        rol.setDescripcion("");
        rol.setEstado(Boolean.TRUE);

        return rol;
    }

    public static List<eRol> obtenerTodos() throws Exception {
        List<eRol> lista = new ArrayList<eRol>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM ROL";
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eRol rol = new eRol();
                rol.setId(rs.getLong(1));
                rol.setNombre(rs.getString(2));
                rol.setDescripcion(rs.getString(3));
                rol.setEstado(rs.getBoolean(4));
                lista.add(rol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static List<eRol> obtenerActivos() throws Exception {
        List<eRol> lista = new ArrayList<eRol>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM ROL WHERE ESTADO = " + 1;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eRol rol = new eRol();
                rol.setId(rs.getLong(1));
                rol.setNombre(rs.getString(2));
                rol.setDescripcion(rs.getString(3));
                rol.setEstado(rs.getBoolean(4));
                lista.add(rol);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static eRol obtenerPorId(Long id) throws Exception {
        eRol rol = new eRol();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM ROL WHERE ID = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                rol.setId(rs.getLong(1));
                rol.setNombre(rs.getString(2));
                rol.setDescripcion(rs.getString(3));
                rol.setEstado(rs.getBoolean(4));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return rol;
    }

    public static List<eRol> obtenerRol(eRol rol) throws Exception {
        List<eRol> lista = new ArrayList<eRol>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM ROL WHERE ID = ID";
        if (rol.getId() != null && !rol.getId().equals(0L)) {
            query += " AND ID = " + rol.getId();
        } else {
            if (rol.getNombre() != null && rol.getNombre().length() > 0) {
                query += " AND NOMBRE LIKE '" + rol.getNombre().toUpperCase() + "'";
            }
            if (rol.getEstado() != null) {
                if (rol.getEstado()) {
                    query += " AND ESTADO = " + 1;
                } else {
                    query += " AND ESTADO = " + 0;
                }
            }
        }
        query += " ORDER BY ID DESC";
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eRol ent = new eRol();
                ent.setId(rs.getLong(1));
                ent.setNombre(rs.getString(2));
                ent.setDescripcion(rs.getString(3));
                ent.setEstado(rs.getBoolean(4));
                lista.add(ent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static boolean guardar(eRol rol) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "INSERT INTO ROL VALUES (?,?,?,?)";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, rol.getId());
            ps.setString(2, rol.getNombre().toUpperCase());
            ps.setString(3, rol.getDescripcion().toUpperCase());
            ps.setBoolean(4, rol.getEstado());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }

    public static boolean actualizar(eRol rol) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "UPDATE ROL SET NOMBRE = ?, DESCRIPCION = ?, ESTADO = ? WHERE ID = ?";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setString(1, rol.getNombre().toUpperCase());
            ps.setString(2, rol.getDescripcion().toUpperCase());
            ps.setBoolean(3, rol.getEstado());
            ps.setLong(4, rol.getId());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        }

        return retorno;
    }
}
